#!/bin/bash
instrumentedapkfile="/home/sheyi/instrumentedApk"
ellahome="/home/sheyi/source-code/ella-master";
finishedapk="/home/sheyi/finishApk"
function expDroid(){
for apk in `ls $instrumentedapkfile`
       do
         echo "start explore $apk"
         java -ea -classpath exp.jar edu.iscas.expdroid.strategy.Main -appid $apk
         echo "finish explore $apk"
         echo "mv the finished apk to other dir"
         mv $instrumentedapkfile/$apk $finishedapk/$apk
         echo "mv $apk finished"
         echo "close ella "
         $ellahome/ella.sh e
         echo "close ella end"
         echo "kill process"
         killprocess
         echo "kill process end "
         echo "rm temp file in device"
         adb shell rm -r /sdcard/expDroid/*
         adb shell rm -r /sdcard/crash/*
         echo "rm files finish"
     done
}

function killprocess(){
#kill expDroid process who use the port 5050  
res=`netstat -anp | grep "5050"| grep "java"| head -n 1` 
if test -z $res 
   then echo "the proces died"
else 
   a=`netstat -anp | grep "5050"| grep "java"| head -n 1| awk '{print $7}' |awk -F '/' '{print $1}'`;
   kill -9 $a
   echo "kill process $a"
fi

# kill expDroidClient process in android device
exist=`adb shell ps| grep "exp.process"|head -n 1|awk '{print $2}'`
if test -z $exist 
   then echo "process of exp died"
else 
   pid=`adb shell ps| grep "exp.process"|head -n 1|awk '{print $2}'`
   adb shell kill -9 $pid
   echo "kill $pid in android device"
fi
}
expDroid
